home *** CD-ROM | disk | FTP | other *** search
- Path: sundog.tiac.net!smayo
- From: smayo@tiac.net (Scott Mayo)
- Newsgroups: comp.lang.c++
- Subject: So I have this pointer-to-member function...
- Date: Tue, 30 Jan 1996 19:50:55 -0500
- Organization: The Internet Access Company
- Message-ID: <d4c1.smail.smayo@tiac.net>
- NNTP-Posting-Host: sunspot.tiac.net
-
- I'm trying to do something in C++ which instinct tells me shouldn't be hard to
- arrange, but the compiler isn't cooperating.
-
- I have a class, call it C, whose purpose in life is to acquire an input
- stream, parse it into tokens, and then have the tokens tell it which member
- functions to call. For token "A" call member function A, and so on. An array
- of struct {char *token; void (C::*f)();} fits the bill well enough to manage
- that.
-
- Now the tricky part. I want to derive class C1 and C2 from C. And I want C1
- (and C2, etc) to have their own such table for mapping tokens to member
- functions; in the absence of a match, I want to go up and search C's table and
- use what I find there.
-
- "Ah," I innocently thought, "I'll just make each array of struct a static
- variable inside the class definition it goes with, and then it will be easy to
- automatically search the right table at the right time."
-
- The compiler will have none of it. It takes one glance at the static struct
- {char*name; void (C::*f)();} list[] = {"A", C::A}; and has a hissy fit at the
- attempt to assign initializers. It's fine outside the class definition; it
- won't work inside.
-
- I can clearly just put the arrays outside the class definitions, and just
- teach the class "search" function to know which array to go after, but it
- seems crass. That array is logically a part of the class behaviour, and if I
- end up creating a lot of these subclasses, I don't want to rely on
- remembering to tell each search function which magic array to use.
-
- Suggestions? Am I missing something simple and useful here? Thanks. Mail
- appreciated, posting might be missed.
-
-